#include "CacheModule.h"

QScopedPointer<CacheModule> CacheModule::ms_instance;
CacheModule::CacheModule(QObject *parent)
    : BaseModule(s_moduleName, parent)
    , m_timer(new QTimer(this))
    , m_cacheCheck(new CacheCheck(this))
    , m_cacheCreate(new CacheCreate(this))
    , m_cacheRecv(new CacheRecv(this))
    , m_cacheUpload(new CacheUpload(this))
{
    //	create table
    m_cacheCreate->createTable();
    //
    connect(this,&CacheModule::pollingIntervalChanged,this,&CacheModule::setTimerPollingInterval);
    //
    connect(m_timer, &QTimer::timeout, this, &CacheModule::recvCheckDateBaseSingal);
    connect(m_timer, &QTimer::timeout, m_cacheCreate, &CacheCreate::createTable);
    //
    connect(m_cacheUpload,&CacheUpload::sendUploadInfo,this,&CacheModule::sendUploadInfo);
    connect(m_cacheUpload,&CacheUpload::sendReuploadInfo,this,&CacheModule::sendReuploadInfo);
}

CacheModule::~CacheModule()
{

}

CacheModule *CacheModule::instance()
{
    if (ms_instance.isNull()) {
        ms_instance.reset(new CacheModule());
    }
    return ms_instance.data();
}


void CacheModule::recvUploadInfo(const QString &info)
{
    m_cacheRecv->recvUploadInfo(info);
}

void CacheModule::recvCommonInfo(const QString &info)
{
    m_cacheRecv->recvCommonInfo(info);
}

void CacheModule::recvTidInfo(const QString &info)
{
    m_cacheRecv->recvTidInfo(info);
}

void CacheModule::recvUploadInfoAndState(const QList<qlonglong> &id, const QList<QString> &info, const QList<QString> &status, const TransmitModule::SendInfoStatus &sendInfoStatus)
{
    m_cacheUpload->recvUploadInfoAndState(id,info,status,sendInfoStatus);
}

void CacheModule::recvReuploadInfoAndState(const QList<qlonglong> &id, const QList<QString> &info, const QList<QString> &status, const TransmitModule::SendInfoStatus &sendInfoStatus)
{
    m_cacheUpload->recvReuploadInfoAndState(id,info,status,sendInfoStatus);
}

void CacheModule::recvUploadSignal(qlonglong num)
{
   m_cacheUpload->recvUploadSingal(num);
}

void CacheModule::recvReuploadSignal(qlonglong num)
{
    m_cacheUpload->recvReuploadSingal(num);
}

void CacheModule::checkTidInfo(const QString &info, bool &tidState)
{
    m_cacheRecv->checkTidInfo(info,tidState);
}

void CacheModule::setTimerPollingInterval(qlonglong sec)
{
    m_timer->stop();
    m_timer->setInterval(secToMsec(sec));
    m_timer->start();
}

void CacheModule::recvCheckDateBaseSingal()
{
    m_cacheCheck->checkDataBase(m_storageDuration,m_cacheRows,m_uploadTimes);
}

void CacheModule::updateProperty(const QJsonObject &conf,const QJsonObject &defaultConf)
{
    if(defaultConf.contains(moduleName())
       && defaultConf[moduleName()].isObject()
      )
    {
        QJsonObject moduleConf;
        QJsonObject moduleDefaultConf = defaultConf[moduleName()].toObject();
        if(conf.contains(moduleName())
            && conf[moduleName()].isObject())
        {
            moduleConf = conf[moduleName()].toObject();
        }
        // 设置检测时长
        qlonglong value;
        if(getValidConfInt(moduleConf,moduleDefaultConf,s_pollingIntervalKey,value) == FunctionSeccess)
        {
            setPollingInterval(value);
            qInfo()<<"module="<<moduleName()<<",property="<<s_pollingIntervalKey<<":"<<value;
        }
        else
        {
            qCritical()<<"module="<<moduleName()<<",property="<<s_pollingIntervalKey<<":read conf fail";
        }
        //	设置存储间隔
        if(getValidConfInt(moduleConf,moduleDefaultConf,s_storageDurationKey,value) == FunctionSeccess)
        {
            setStorageDuration(value);
            qInfo()<<"module="<<moduleName()<<",property="<<s_storageDurationKey<<":"<<value;
        }
        else
        {
            qCritical()<<"module="<<moduleName()<<",property="<<s_storageDurationKey<<":read conf fail";
        }
        //	设置存储行数
        if(getValidConfInt(moduleConf,moduleDefaultConf,s_cacheRowsKey,value) == FunctionSeccess)
        {
            setCacheRows(value);
            qInfo()<<"module="<<moduleName()<<",property="<<s_cacheRowsKey<<":"<<value;
        }
        else
        {
            qCritical()<<"module="<<moduleName()<<",property="<<s_cacheRowsKey<<":read conf fail";
        }
        //	设置上传次数
        if(getValidConfInt(moduleConf,moduleDefaultConf,s_uploadTimesKey,value) == FunctionSeccess)
        {
            setUploadTimes(value);
            qInfo()<<"module="<<moduleName()<<",property="<<s_uploadTimesKey<<":"<<value;
        }
        else
        {
            qCritical()<<"module="<<moduleName()<<",property="<<s_uploadTimesKey<<":read conf fail";
        }
    }
    else
    {
            qCritical()<<"module="<<moduleName()<<":update property fail";
    }
}

void CacheModule::printConf()
{
    qDebug()<<"PollintInterval:"<<m_pollingInterval;
    qDebug()<<"CacheSize:"<<m_cacheSize;
    qDebug()<<"StorageDuration:"<<m_storageDuration;
    qDebug()<<"CacheRows:"<<m_cacheRows;
    qDebug()<<"SingleLineBytes:"<<m_singleLineBytes;
    qDebug()<<"UploadTimes:"<<m_uploadTimes;

}
